package com.huawei.bigdata.kafka.service;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huawei.bigdata.kafka.temp.TestProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.*;
import java.util.zip.GZIPInputStream;

@Service
@Slf4j
public class FileReadService {

    @Value("${ext.comm.config.file-parent-path}")
    private String fileParentPath;

    @Autowired
    private TestProducer testProducer;

    private static final ObjectMapper objectMapper = new ObjectMapper();
    // 配置支持未识别字段，避免解析失败
    static {
        StreamReadConstraints constraints = StreamReadConstraints.builder()
                .maxStringLength(Integer.MAX_VALUE) // 约21亿字符，接近无限大
                .build();

        objectMapper.getFactory()
                .setStreamReadConstraints(constraints);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    }
    public static String readFile(String path) {
//        String file = "D:/liyx/ideaWorkspace/shuzidianwang/efile_parse_szdw/xml_parse_dd/src/main/resources/content1.txt";

        try(InputStreamReader reader = new InputStreamReader(new FileInputStream(path))) {
            JsonNode rootNode = objectMapper.readTree(reader);
            String s = objectMapper.writeValueAsString(rootNode);
//            System.out.println(s);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public void sendMsg(){
        Map<String, String> map = new HashMap<String, String>();
        map.put("SWYF_GRC_COS_ALARM_PW_0401", "GRC_COS_ALARM_PW_0401.json");
        map.put("SWYF_GRC_COS_ALARM_ZW_0401", "GRC_COS_ALARM_ZW_0401.json");
        map.put("SWYF_GRC_OCS_EFILE_PW_0401", "GRC_OCS_EFILE_PW_0401.json");
        map.put("SWYF_GRC_OCS_EFILE_ZW_0401", "GRC_OCS_EFILE_ZW_0401.json");
        map.put("SWYF_GRC_OCS_TRANS_TRIP_PW", "GRC_OCS_TRANS_TRIP_PW.json");
        map.put("SWYF_GRC_OCS_TRANS_TRIP_ZW", "GRC_OCS_TRANS_TRIP_ZW.json");
        map.put("SWYF_GRC_SOE_ALARM_PW_0401", "GRC_SOE_ALARM_PW_0401.json");
        map.put("SWYF_GRC_SOE_ALARM_ZW_0401", "GRC_SOE_ALARM_ZW_0401.json");


        map.entrySet().forEach(entry -> {
            String path = fileParentPath + entry.getValue();
            String s = readFile(path);
            // 获取随机数 Int

            testProducer.send(entry.getKey(), String.valueOf(new Random().nextInt(1000)), s, 1);
        });

    }

    @Scheduled(initialDelay = 10000, fixedRate = 30000)
    public void readFile() {
        log.info("开始执行定时任务: {}", new Date());
        sendMsg();
    }

}
